package com.firewolf.springcloud.controller;

import com.firewolf.springcloud.UserService;
import com.firewolf.springcloud.pojo.User;
import com.firewolf.springcloud.vo.ResponseVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.jws.Oneway;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：刘兴
 * 时间:19/2/12
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    public User test(){
        User u = restTemplate.getForObject("http://provider/user", User.class);
        return u;
    }

    @GetMapping("/add")
    public ResponseVO add(){

        User u = new User(200,"八节");
//        ResponseEntity<ResponseVO> responseEntity = restTemplate.postForEntity("http://provider/user", u, ResponseVO.class);
//        ResponseVO body = responseEntity.getBody(); //返回结果里面的数据

        ResponseVO responseVO = userService.addUser(u);
        return  responseVO;
    }



    @GetMapping("/add2")
    public ResponseVO add2(){

//        ResponseEntity<ResponseVO> responseEntity = restTemplate.postForEntity("http://provider/user", u, ResponseVO.class);
//        ResponseVO body = responseEntity.getBody(); //返回结果里面的数据

        ResponseVO responseVO = restTemplate.postForObject("http://provider/user/a?id=1&name='悟空'", null, ResponseVO.class);
//        ResponseVO responseVO = restTemplate.postForObject("http://provider/user", new User(1,"AAA"), ResponseVO.class);

        restTemplate.postForObject("http://provider/user/a?id={1}&name={2}",null,ResponseVO.class,1,"八节");

        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("myname","王五");

        restTemplate.postForObject("http://provider/user/a?id={id}&name={myname}",null,ResponseVO.class,map);

        return  responseVO;
    }

    @GetMapping("/get")
    public List<User> get(){
        User u = new User(1,"悟净");
        List forObject = restTemplate.getForObject("http://provider/user/b?id=5&name=章程", List.class);
        return  forObject;
    }


}
